package com.example.dao.impl;

import com.example.dao.IAccountDao;
import com.example.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 持久层接口实现类
 */
@Repository
public class AccountDaoImpl implements IAccountDao {

    @Autowired
    private QueryRunner queryRunner;

    public void setQueryRunner(QueryRunner queryRunner) {
        this.queryRunner = queryRunner;
    }

    public List<Account> findAllAccount() {
        try {
            return queryRunner.query("select * from account ", new BeanListHandler<Account>(Account.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Account findAccountById(Integer accountId) {
        try {
            return queryRunner.query("select * from account where id=?", new BeanHandler<Account>(Account.class),accountId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void saveAccount(Account account) {
        try {
            queryRunner.update("insert into account(name , money) values (?,?) ", account.getName(), account.getMoney());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void batchSave(List<Account> ats) {
        try {
            Object[][] objects = new Object[ats.size()][];
            for (int i = 0; i < ats.size(); i++) {
                Account a = ats.get(i);
                objects[i] = new Object[2];
                objects[i][0] = a.getName();
                objects[i][1] = a.getMoney();
            }
            queryRunner.batch("insert into account(name , money) values (?,?) ", objects);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void updateAccount(Account account) {
        try {
            queryRunner.update("update account set name=? , money=? where id=? ", account.getName(), account.getMoney(), account.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void deleteAccount(Integer accountId) {
        try {
            queryRunner.update("delete from account where id=? ", accountId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Account findAccoutByName(String name) {
        try {
            List<Account> ls = queryRunner.query("select * from account where name=?", new BeanListHandler<Account>(Account.class),name);
            if(ls==null || ls.isEmpty()){
                return null;
            }
            if (ls.size()>1){
                throw new RuntimeException("不能有多个账户");
            }
            return ls.get(0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
